import java.io.FileInputStream;
import java.io.FileNotFoundException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;


public class Test {

    public static void main(String[] args) throws Exception {
        InputSource is = new InputSource(new FileInputStream("d:/test.xml"));
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(is);
        System.err.println(doc.getXmlEncoding());
        Element root = doc.getDocumentElement();
        String s = getChildNodeValue(root, "a", true);
        System.out.println(s);
    }
    
    public static String getChildNodeValue(final Node parent, final String tagName,
            final boolean searchDeeper) throws Exception {
        if (null == parent) {
            return null;
        }
        String value = null;
        NodeList childrenList = parent.getChildNodes();
        int childrenCnt = childrenList.getLength();
        for (int i = 0; i < childrenCnt; i++) {
            Node child = childrenList.item(i);
            if (child.getNodeName().equals(tagName)) {
                // タグがある
                Node element = child.getFirstChild();
                if (null != element) {
                    value = (null == element.getNodeValue()
                            ? "" : new String(element.getNodeValue().getBytes(), "UTF-8"));
                } else {
                    return "";
                }
            } else {
                if (searchDeeper) {
                    value = getChildNodeValue(child, tagName, searchDeeper);
                }
            }
            if (null != value) {
                break;
            }
        }
        return value;
    }
}
